From: Yehuda Katz Date: Wed, 28 May 2014 01:33:06 +0000 (-0700) Subject: Initial pass at a Shell X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~1044 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=574829e9f761252a4c8e184f40f36fff93b92892;p=cargo.git Initial pass at a Shell --- diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index a5d98142b..ea4fb0602 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -6,33 +6,32 @@ use std::io::IoResult; use std::io::stdio::StdWriter; pub struct ShellConfig { - color: bool, - verbose: bool + pub color: bool, + pub verbose: bool, + pub tty: bool } -enum AdequateTerminal { - NoColor(BasicTerminal), - Color(Box>) +enum AdequateTerminal { + NoColor(BasicTerminal), + Color(Box>) } -pub struct Shell { - terminal: AdequateTerminal, +pub struct Shell { + terminal: AdequateTerminal, config: ShellConfig } -impl Shell { - fn create(out: StdWriter, config: ShellConfig) -> Option { - let term = if out.isatty() { - let term: Option> = Terminal::new(out); - term.map(|t| Color(box t)) +impl Shell { + pub fn create(out: T, config: ShellConfig) -> Option> { + if config.tty { + let term: Option> = Terminal::new(out); + term.map(|t| Shell { terminal: Color(box t as Box>), config: config }) } else { - Some(NoColor(BasicTerminal { writer: out })) - }; - - term.map(|term| Shell { terminal: term, config: config }) + Some(Shell { terminal: NoColor(BasicTerminal { writer: out }), config: config }) + } } - pub fn verbose(&mut self, callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> { + pub fn verbose(&mut self, callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> { if self.config.verbose { return callback(self) } @@ -50,9 +49,9 @@ impl Shell { } } -impl Terminal for Shell { - fn new(out: StdWriter) -> Option { - Shell::create(out, ShellConfig { color: true, verbose: false }) +impl Terminal for Shell { + fn new(out: T) -> Option> { + Shell::create(out, ShellConfig { color: true, verbose: false, tty: false }) } fn fg(&mut self, color: color::Color) -> IoResult { @@ -90,18 +89,18 @@ impl Terminal for Shell { } } - fn unwrap(self) -> StdWriter { - fail!("Can't unwrap a Shell") + fn unwrap(self) -> T { + fail!("Can't unwrap a Shell"); } - fn get_ref<'a>(&'a self) -> &'a StdWriter { + fn get_ref<'a>(&'a self) -> &'a T { match self.terminal { Color(ref c) => c.get_ref(), NoColor(ref n) => n.get_ref() } } - fn get_mut<'a>(&'a mut self) -> &'a mut StdWriter { + fn get_mut<'a>(&'a mut self) -> &'a mut T { match self.terminal { Color(ref mut c) => c.get_mut(), NoColor(ref mut n) => n.get_mut() @@ -109,7 +108,7 @@ impl Terminal for Shell { } } -impl Writer for Shell { +impl Writer for Shell { fn write(&mut self, buf: &[u8]) -> IoResult<()> { match self.terminal { Color(ref mut c) => c.write(buf), @@ -129,7 +128,7 @@ pub struct BasicTerminal { writer: T } -impl Terminal for BasicTerminal { +impl Terminal for BasicTerminal { fn new(out: T) -> Option> { Some(BasicTerminal { writer: out }) } @@ -167,7 +166,7 @@ impl Terminal for BasicTerminal { } } -impl Writer for BasicTerminal { +impl Writer for BasicTerminal { fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.writer.write(buf) } diff --git a/tests/support.rs b/tests/support.rs index 9dee66e6e..131bf9891 100644 --- a/tests/support.rs +++ b/tests/support.rs @@ -1,4 +1,5 @@ // use std::io::fs::{mkdir_recursive,rmdir_recursive}; +use std; use std::io; use std::io::fs; use std::io::process::{ProcessOutput,ProcessExit}; @@ -8,6 +9,7 @@ use std::str; use std::vec::Vec; use std::fmt::Show; use ham = hamcrest; +use cargo::core::shell; use cargo::util::{process,ProcessBuilder,CargoError}; use cargo::util::result::ProcessError; @@ -246,6 +248,30 @@ pub fn execs() -> Box { } } +#[deriving(Clone,Eq)] +struct ShellWrites { + expected: String +} + +impl ham::SelfDescribing for ShellWrites { + fn describe(&self) -> String { + format!("`{}` written to the shell", self.expected) + } +} + +impl<'a> ham::Matcher<&'a mut shell::Shell> for ShellWrites { + fn matches(&self, actual: &mut shell::Shell) -> ham::MatchResult { + use term::Terminal; + + let actual = std::str::from_utf8_lossy(actual.get_ref().get_ref()).to_str(); + ham::expect(actual == self.expected, actual) + } +} + +pub fn shell_writes(string: T) -> Box { + box ShellWrites { expected: string.to_str() } +} + pub trait ResultTest { fn assert(self) -> T; } @@ -258,3 +284,23 @@ impl ResultTest for Result { } } } + +impl ResultTest for Option { + fn assert(self) -> T { + match self { + Some(val) => val, + None => fail!("Option was None") + } + } +} + +pub trait Tap { + fn tap(mut self, callback: |&mut Self|) -> Self; +} + +impl Tap for T { + fn tap(mut self, callback: |&mut T|) -> T { + callback(&mut self); + self + } +} diff --git a/tests/tests.rs b/tests/tests.rs index 9e3684ba7..495adba1e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,6 +1,7 @@ #![feature(macro_rules)] #![allow(deprecated_owned_vector)] +extern crate term; extern crate cargo; extern crate hamcrest; @@ -16,3 +17,4 @@ macro_rules! test( mod support; mod test_cargo_compile; +mod test_shell;